package com.zjw.zy.bean;

import android.os.Parcel;
import android.os.Parcelable;

import androidx.annotation.NonNull;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.PrimaryKey;

import com.zjw.zy.smi.ICopy;
import com.zjw.zy.utils.TimeUtils;

import java.util.Date;
import java.util.Objects;

@Entity(tableName = "recorder")
public class RecorderItem implements Parcelable, ICopy<RecorderItem> {


    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "id", typeAffinity = ColumnInfo.INTEGER) //对应列名，及其类型
    public final int id;
    @ColumnInfo(name = "date", typeAffinity = ColumnInfo.INTEGER) //对应列名，及其类型
    public final long date;
    @ColumnInfo(name = "title", typeAffinity = ColumnInfo.TEXT) //对应列名，及其类型
    public String title;
    @ColumnInfo(name = "duration", typeAffinity = ColumnInfo.INTEGER) //对应列名，及其类型
    public final int duration;
    @ColumnInfo(name = "path", typeAffinity = ColumnInfo.TEXT) //对应列名，及其类型
    public final String path;

    public RecorderItem(int id, String title, int duration, long date, String path) {
        this.id = id;
        this.title = title;
        this.duration = duration;
        this.date = date;
        this.path = path;
    }


    protected RecorderItem(Parcel in) {
        id = in.readInt();
        date = in.readLong();
        title = in.readString();
        duration = in.readInt();
        path = in.readString();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(id);
        dest.writeLong(date);
        dest.writeString(title);
        dest.writeInt(duration);
        dest.writeString(path);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<RecorderItem> CREATOR = new Creator<RecorderItem>() {
        @Override
        public RecorderItem createFromParcel(Parcel in) {
            return new RecorderItem(in);
        }

        @Override
        public RecorderItem[] newArray(int size) {
            return new RecorderItem[size];
        }
    };

    public int getId() {
        return id;
    }

    public String getPath() {
        return path;
    }

    public String getTitle() {
        return title;
    }

    public int getDuration() {
        return duration;
    }

    public long getDate() {
        return date;
    }

    public String getDateOfString() {
        return TimeUtils.formatDate(new Date(date), "yyyy/MM/dd HH:mm");
    }

    public void setTitle(String title) {
        this.title = title;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        RecorderItem that = (RecorderItem) o;
        return id == that.id && date == that.date && duration == that.duration
                && Objects.equals(title, that.title) && Objects.equals(path, that.path);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, date, title, duration, path);
    }

    @NonNull
    @Override
    public RecorderItem copy() {
        return new RecorderItem(id, title, duration, date, path);
    }
}
